
(async function() {
    "use strict";

    function require(url) {
        return new Promise((resolve, reject) => {
            var head = document.head || (document.getElementsByTagName("head")[0] || document.documentElement);
            var script = document.createElement("script");
            script.onload = () => {
                resolve();
            };
            script.onerror = () => {
                reject();
            };
            script.async = true;
            script.src = url;
            head.appendChild(script);
        })
    }

    require("https://res2.wx.qq.com/open/js/jweixin-1.6.0.js")
    .then(() => {
        fetch(
            "/api/wx/signature",
            {
                method: "POST",
                headers: {
                    "Content-Type": "application/json",
                },
                body: JSON.stringify({
                    url: window.location.href
                })
            }
        ).then(async response => {
            const result = await response.json();
            if (result.type === "failed")
            {
                console.error("wx.js:", result.message);
                return;
            }

            const signatureInfo = result.data;
            wx.isReady = false;
            wx.config({
                debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。
                appId: 'wx4de03f4789e8d032', // 必填，公众号的唯一标识
                timestamp: signatureInfo.timestamp, // 必填，生成签名的时间戳
                nonceStr: signatureInfo.nonceStr, // 必填，生成签名的随机串
                signature: signatureInfo.signature,// 必填，签名
                jsApiList: [
                    // "updateAppMessageShareData",
                    // "updateTimelineShareData",
                    "chooseImage",
                    "previewImage",
                    "getLocalImgData"
                ] // 必填，需要使用的JS接口列表
            });
            wx.ready(function() {
                wx.isReady = true;

                // wx.updateAppMessageShareData({ 
                //     title: document.title, // 分享标题
                //     desc: document.querySelector('meta[name="description"]').content, // 分享描述
                //     link: window.location.href, // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
                //     imgUrl: "http://cyue.net/favicon.png", // 分享图标
                //     success: function () {
                //         // 设置成功
                //     }
                // });
                // wx.updateTimelineShareData({ 
                //     title: document.title, // 分享标题
                //     link: window.location.href, // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
                //     imgUrl: "http://cyue.net/favicon.png", // 分享图标
                //     success: function () {
                //       // 设置成功
                //     }
                // });


                const imgElements = document.querySelectorAll("img");
                imgElements.forEach(element => {
                    if (/data:image\/[^;]*base64/i.test(element.src))
                        return;
                    if (element.src.includes("lsky.cyue.net"))
                        return;
                    if (element.alt === "RSS" || element.alt === "share")
                        return;

                    const previewImage = function(_e) {
                        let url = element.src;
                        // if (!url.includes("http"))
                        //     url = "https://cyue.net" + url;
                        wx.previewImage({
                            current: url,
                            urls: [url]
                        });
                    }
                    
                    element.addEventListener("click", previewImage);
                });

            });
        });
    });
    
})()






